package com.jy.rhin.application.assembler;

import cn.org.chiss.rhin._2015.base.*;
import cn.org.chiss.rhin._2015.base.object.MainID;
import cn.org.chiss.rhin._2015.base.object.ObjectStatus;
import cn.org.chiss.rhin._2015.base.object.ObjectStatusPrimitive;
import cn.org.chiss.rhin._2015.base.terminology.Contains;
import cn.org.chiss.rhin._2015.base.terminology.ValueSet;
import cn.org.chiss.rhin._2015.base.terminology.ValueSetCompose;
import cn.org.chiss.rhin._2015.base.terminology.ValueSetExpansion;
import cn.org.chiss.rhin._2015.registration.terminology.QueryPayload;
import cn.org.chiss.rhin._2015.registration.terminology.ValueSetQueryRequest;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.WSSYBMXTDaiMaDto;
import com.jy.api.misc.dto.ZhiJiCXDto;
import com.jy.api.misc.dto.ZhiYuDto;
import com.jy.framework.base.utils.Converter;
import com.jy.framework.base.utils.DateUtil;
import com.jy.rhin.utils.RhinUtils;
import org.springframework.util.StringUtils;

import java.lang.String;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: lww
 * @Date: 2022/5/24
 */
public class ValueSetAssembler {

    private ValueSetAssembler() {
    }

    /**
     * ws入参转术语系统入参
     *
     * @param message
     * @return
     */
    public static ZhiJiCXDto toDto(ValueSetQueryRequest message) {
        QueryPayload queryPayload = message.getQueryPayload();
        ZhiJiCXDto zhiJiCxDto = new ZhiJiCXDto();
        /** 显示名称包含 */
        zhiJiCxDto.setDisplayNameContain(RhinUtils.getRhinStringValue(queryPayload.getDisplayNameContain()));
        /** 组别名称包含 */
        zhiJiCxDto.setGroupContain(RhinUtils.getRhinStringValue(queryPayload.getGroupContain()));
        /** 发布者名称包含 */
        zhiJiCxDto.setPublisherContain(RhinUtils.getRhinStringValue(queryPayload.getPublisherContain()));
        if (null != (queryPayload.getExpirationDateAfter())) {
            /** 创建日期之后 */
            zhiJiCxDto.setExpirationDateAfter(queryPayload.getExpirationDateAfter().getValue());
        }
        if (null != (queryPayload.getExpirationDateBefore())) {
            /** 创建日期之前 */
            zhiJiCxDto.setExpirationDateBefore(queryPayload.getExpirationDateBefore().getValue());
        }
        if (null != (queryPayload.getCreationDateBefore())) {
            /** 失效日期之后 */
            zhiJiCxDto.setCreationDateBefore(queryPayload.getCreationDateBefore().getValue());
        }
        if (null != (queryPayload.getCreationDateAfter())) {
            /** 失效日期之前 */
            zhiJiCxDto.setCreationDateAfter(queryPayload.getCreationDateAfter().getValue());
        }
        if (null != (queryPayload.getGroupId())) {
            /** 组别标识 */
            zhiJiCxDto.setGroupId(queryPayload.getGroupId().getValue());
        }
        if (null != (queryPayload.getIncludeObsolete())) {
            /** 是否包含已过期值集编码 */
            zhiJiCxDto.setIncludeObsolete(queryPayload.getIncludeObsolete().getValue());
        }
        if (null != (queryPayload.getReference())) {
            /** 引用编码系统 OID */
            zhiJiCxDto.setReference(queryPayload.getReference().getValue());
        }
        return zhiJiCxDto;
    }

    public static ValueSet toValueSet(ZhiYuDto data) {
        ValueSet valueSet = new ValueSet();
        Period period = new Period();
        /** 设置生效时间 */
        if(data.getShengXiaoSJ()!= null){
            period.setStart(new DateTime().setValue(getDateString(data.getShengXiaoSJ()).replaceAll("-","")));
        }
        /** 设置失效时间 */
        if (null != data.getShiXiaoSJ()) {
            period.setEnd(new DateTime().setValue(getDateString(data.getShiXiaoSJ())));
        } else {
            period.setEnd(new DateTime().setValue(DateUtil.getYYMMDD(new Date()).replaceAll("-","")));
        }
        /** 编码映射的有效时间段 */
        valueSet.setEffectiveTime(period);
        /** 设置标识符 */
        valueSet.setId((MainID) new MainID().setValue(data.getZhiJiBSF()));
        /** 发布者 */
        valueSet.setPublisher(setValue(data.getFaBuJGMC()));
        /** 编码映射描述 */
        valueSet.setDescription(setValue(data.getMiaoShu()));
        /** 设置名称 */
        valueSet.setName(setValue(data.getZhiJiMC()));
        /** 设置状态 */
        valueSet.setStatusCode(new ObjectStatus().setValue(ObjectStatusPrimitive.fromValue(data.getStatus()!= null ? data.getStatus().toString():"2")));
        valueSet.setCompose(new ValueSetCompose().set_import(Collections.singletonList(new OID().setValue(data.getZhiJiBSF()))))
                .setExpansion(setValueSetExpansion(data));

//        valueSet.setExpansion(setValueSetExpansion(data));
        return valueSet;
    }

    private static ValueSetExpansion setValueSetExpansion(ZhiYuDto data) {
        ValueSetExpansion expansion = new ValueSetExpansion();
        expansion.setVersion(setValue("V1.0"));
        expansion.setEffectiveTime(new DateTime().setValue(Converter.toString(data.getFaBuSJ(), "yyyyMMdd")));

        Contains contains = new Contains();
        List<Coding> codings = new ArrayList<>();
        for (DaiMaDto dm : data.getDaiMaList()) {
            Coding cd = new Coding();
            cd.setCode(new Code().setValue(dm.getCode()));
            cd.setDisplay(setValue(dm.getValue()));
            cd.setSystem(new URI().setValue(dm.getBianMaXTBSF()));
            cd.setSystemName(setValue(dm.getBianMaXTMC()));
            cd.setVersion(setValue("V1.0"));
            codings.add(cd);
        }
        contains.setCode(codings);
        expansion.setContains(contains);
        return expansion;
    }

    public static ValueSet toValueSet(WSSYBMXTDaiMaDto data) {
        ValueSet valueSet = new ValueSet();
        if (Objects.isNull(data)) {
            return valueSet;
        }
        /** id */
        valueSet.setId((MainID) new MainID().setValue(data.getBiaoShiFu()));
        /** 状态码 */
        valueSet.setStatusCode(new ObjectStatus().setValue(ObjectStatusPrimitive.ACTIVE));
        /** 名称 */
        valueSet.setName(setValue(data.getLeiBieMC()));

        /** 发布者 */
        valueSet.setPublisher(setValue(data.getFaBuJGMC()));
        valueSet.setDescription(setValue(data.getMiaoShu()));
        Period period = new Period();
        if(null != data.getShengXiaoSJ()){
            period.setStart(new DateTime().setValue(getDateString(data.getShengXiaoSJ())));
        }
        if (null != data.getShiXiaoSJ()) {
            period.setEnd(new DateTime().setValue(getDateString(data.getShiXiaoSJ())));
        }
        valueSet.setEffectiveTime(period);

        /** 设置展开模式对应的版本信息 */
        ValueSetExpansion valueSetExpansion = new ValueSetExpansion();
        Contains contains = new Contains();
        List<Coding> codingList = new ArrayList();
        List<OID> list = new ArrayList<>();
        ValueSetCompose valueSetCompose = new ValueSetCompose();
        for (DaiMaDto daiMaDto : data.getDaiMaVoList()) {
            Coding coding = new Coding();
            coding.setDisplay(setValue(daiMaDto.getValue()));
            coding.setCode(new Code().setValue(daiMaDto.getCode()));
            coding.setSystem(new URI().setValue(data.getBiaoShiFu()));
            coding.setSystemName(setValue(data.getLeiBieMC()));
            coding.setVersion(setValue("V1.0"));
            codingList.add(coding);
            list.add(new OID().setValue(daiMaDto.getBianMaXTBSF()));
        }
        /** 设置值集组成 */
        valueSetCompose.set_import(list.stream().distinct().collect(Collectors.toList()));
        contains.setCode(codingList);
        valueSet.setCompose(valueSetCompose);
        valueSetExpansion.setContains(contains);
        /** 设置版本信息 */
        valueSetExpansion.setVersion(setValue(data.getBanBen()!= null ? data.getBanBen().toString():null));
        valueSetExpansion.setEffectiveTime(new DateTime().setValue(Converter.toDateTimeStr(data.getChuangJianSJ())));
        valueSet.setExpansion(valueSetExpansion);
        return valueSet;
    }

    /**
     * 获取字符串类型时间
     *
     * @param date
     * @return
     */
    private static String getDateString(LocalDate date) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return dtf.format(date);
    }

    /**
     * 获取字符串类型时间
     *
     * @param date
     * @return
     */
    public static String format(LocalDateTime date) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:MM:ss");
        return df.format(date);
    }

    private static cn.org.chiss.rhin._2015.base.String setValue(String value) {
        return new cn.org.chiss.rhin._2015.base.String().setValue(value);
    }

}
